// Plugin para el manejo de tablas

// definición de la función
$.fn.archetypeTree = function(options) {
    // puede recibir un array de parámetros nombrados  
    // invocamos a una función genérica que hace el merge   
    // entre los recibidos y los de por defecto
    var opts = $.extend({}, $.fn.archetypeTree.defaults, options);

    this.each(showArchetypeTree(opts,this));
};

function showArchetypeTree(opts,id) 
{   
    var treeJSON = "";
    var dnd = getDND(opts);
    var types = getTypes(opts);
    
    if(opts.enableLoading){    
        var gif = "/" + opts.relativePath + "/js/jstree/themes/default/icons/loading.gif";
        id.html('<p><img src='+ gif + ' /></p>');        
    }    
    $.ajax({
        cache: false,
        url: "/" + opts.relativePath + "/" + opts.controller + "/" + opts.action,
        data:opts.params,
        success: function(msg) {
          
            treeJSON = msg;          
            /*
          var str = JSON.stringify(treeJSON);          
          $("#divArquetipoPrueba").html(str);
             */
         
            id.jstree({
                "json_data" : {
                    "data": [treeJSON]                     
                },                            
                "themes" : {
                    "theme" : "default",                     
                    "dots" : true,
                    "icons" : true
                },
                "types" : {                    
                    "types" : types,
                    "use_data": true 
                },
                "dnd" : dnd,
                "crrm" : { move : { check_move : function (m) { return false; } } },
                "plugins" : [ "themes", "json_data", "ui", "types","crrm","dnd"],
                "core":{
                    "animation":0
                }
            })
            .bind("select_node.jstree", function (event, data) {	            
                opts.onclick(data);
            });
        },    
        error: function(msg) {               
            id.html(msg.responseText);
        }     
    });	
}

// definimos los parámetros junto con los valores por defecto de la función
$.fn.archetypeTree.defaults = {
    relativePath:"TemplateDesign-OpenEHRGen",        
    controller:"controller",
    action:"action",
    params:{},
    dnd:false,
    functionDND:function(data){},    
    onclick:function(data){},
    enableLoading:true,
    types:0
};


/********* Auxiliares ***********/
function getDND(opts){
    var dnd;
    if(opts.dnd != false){    
        dnd = {
            "drop_finish" : function (data) {                 
                opts.functionDND(data);
                return false;
            },                                               
            "drop_check" : function (data) {                            
                return (data.o.data("tipo") == "hoja");                
            },
            "drop_target":opts.dnd,
            "drag_target":false,
            "drag_check" : function (data) {
                return {
                    after : false, 
                    before : false, 
                    inside : false
                };            
            }
            
        };                
    } else {
        dnd = {
            "drag_target":false,
            "drag_check" : function (data) {
                return {
                    after : false, 
                    before : false, 
                    inside : false
                };            
            }
        };
    }
    
    return dnd;
}

function getTypes(opts){
    var types;
    switch(opts.types){
        case 0:
            types = {                         
                "folder" : {
                    "icon" : {
                        "image" : "/" + opts.relativePath + "/js/jstree/themes/default/icons/yellow_folder.png"
                    },                   
                    "hover_node" : true,
                    "select_node" : function () {
                        return true;
                    }
                },
                "emptyfolder" : {
                    "icon" : {
                        "image" : "/" + opts.relativePath + "/js/jstree/themes/default/icons/generic_folder.png"
                    },                    
                    "hover_node" : true,
                    "select_node" : function () {
                        return true;
                    }
                },
                "archetype" : {
                    "icon" : {
                        "image" : "/" + opts.relativePath + "/js/jstree/themes/default/icons/default.png"
                    },         
                    "hover_node" : true,
                    "select_node" : function () {
                        return true;
                    }
                } 
            };
            break;
        case 1:
            types = {
                "action" : {
                    "icon" : {
                        "image" : "/" + opts.relativePath + "/js/jstree/themes/default/icons/action.png"
                    },
                    "hover_node" : true,
                    "select_node" : function () {
                        return true;
                    }
                },
                "cluster" : {
                    "icon" : {
                        "image" : "/" + opts.relativePath + "/js/jstree/themes/default/icons/cluster.png"
                    },
                    "hover_node" : true,
                    "select_node" : function () {
                        return true;
                    }
                },
                "composition" : {
                    "icon" : {
                        "image" : "/" + opts.relativePath + "/js/jstree/themes/default/icons/composition.png"
                    },
                    "hover_node" : true,
                    "select_node" : function () {
                        return true;
                    }
                },
                "count" : {
                    "icon" : {
                        "image" : "/" + opts.relativePath + "/js/jstree/themes/default/icons/count.png"
                    },
                    "hover_node" : true,
                    "select_node" : function () {
                        return true;
                    }
                },
                "datetime" : {
                    "icon" : {
                        "image" : "/" + opts.relativePath + "/js/jstree/themes/default/icons/datetime.png"
                    },
                    "hover_node" : true,
                    "select_node" : function () {
                        return true;
                    }
                },
                "duration" : {
                    "icon" : {
                        "image" : "/" + opts.relativePath + "/js/jstree/themes/default/icons/duration.png"
                    },
                    "hover_node" : true,
                    "select_node" : function () {
                        return true;
                    }
                },
                "element" : {
                    "icon" : {
                        "image" : "/" + opts.relativePath + "/js/jstree/themes/default/icons/element.png"
                    },
                    "hover_node" : true,
                    "select_node" : function () {
                        return true;
                    }
                },
                "evaluation" : {
                    "icon" : {
                        "image" : "/" + opts.relativePath + "/js/jstree/themes/default/icons/evaluation.png"
                    },
                    "hover_node" : true,
                    "select_node" : function () {
                        return true;
                    }
                },                        
                "instruction" : {
                    "icon" : {
                        "image" : "/" + opts.relativePath + "/js/jstree/themes/default/icons/instruction.png"
                    },
                    "hover_node" : true,
                    "select_node" : function () {
                        return true;
                    }
                },                         
                "observation" : {
                    "icon" : {
                        "image" : "/" + opts.relativePath + "/js/jstree/themes/default/icons/observation.png"
                    },
                    "hover_node" : true,
                    "select_node" : function () {
                        return true;
                    }
                },                            
                "ordinal" : {
                    "icon" : {
                        "image" : "/" + opts.relativePath + "/js/jstree/themes/default/icons/ordinal.png"
                    },
                    "hover_node" : true,
                    "select_node" : function () {
                        return true;
                    }
                },                          
                "quantity" : {
                    "icon" : {
                        "image" : "/" + opts.relativePath + "/js/jstree/themes/default/icons/quantity.png"
                    },
                    "hover_node" : true,
                    "select_node" : function () {
                        return true;
                    }
                }, 
                "section" : {
                    "icon" : {
                        "image" : "/" + opts.relativePath + "/js/jstree/themes/default/icons/section.png"
                    },
                    "hover_node" : true,
                    "select_node" : function () {
                        return true;
                    }
                },                                                 
                "slot_cluster" : {
                    "icon" : {
                        "image" : "/" + opts.relativePath + "/js/jstree/themes/default/icons/slot_cluster.png"
                    },
                    "hover_node" : true,
                    "select_node" : function () {
                        return true;
                    }
                },
                "structure" : {
                    "icon" : {
                        "image" : "/" + opts.relativePath + "/js/jstree/themes/default/icons/structure.png"
                    },
                    "hover_node" : true,
                    "select_node" : function () {
                        return true;
                    }
                },                         
                "text" : {
                    "icon" : {
                        "image" : "/" + opts.relativePath + "/js/jstree/themes/default/icons/text.png"
                    },
                    "hover_node" : true,
                    "select_node" : function () {
                        return true;
                    }
                }, 
                "truefalse" : {
                    "icon" : {
                        "image" : "/" + opts.relativePath + "/js/jstree/themes/default/icons/truefalse.png"
                    },
                    "hover_node" : true,
                    "select_node" : function () {
                        return true;
                    }
                },
                "default" : {
                    "icon" : {
                        "image" : "/" + opts.relativePath + "/js/jstree/themes/default/icons/default.png"
                    },
                    "hover_node" : true,
                    "select_node" : function () {
                        return true;
                    }
                } 
            };
            break;
        default:
            types = {};
            break;              
    }
    
    return types;
}


